package com.wuxuehua.myapp;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 吴学华
 * Date: 17-10-20
 * Time: 上午11:39
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class Pizhu {

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;

    @Autowired
    HistoryService historyService;

    @Test
    public void sta() {
        String taskId = "20013";
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        String processInstanceId = task.getProcessInstanceId();

        List<Comment> comments = taskService.getProcessInstanceComments(processInstanceId);
        for (Comment c : comments) {
            System.out.println("用户 :" + c.getId() + "内容 :" + c.getFullMessage());
        }
    }

    @Test
    public void sta1() {
        String taskId = "20013";
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        String processInstanceId = task.getProcessInstanceId();
        //使用流程实例ID，查询历史任务，获取历史任务对应的每个任务ID
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery()//历史任务表查询
                .processInstanceId(processInstanceId)//使用流程实例ID查询
                .list();
//遍历集合，获取每个任务ID
        for (HistoricTaskInstance h: list) {
            Date dueDate = h.getTime();
            String name = h.getName();
            String id = h.getId();
            String assignee = h.getAssignee();
            List<Comment> comments = taskService.getTaskComments(id);
            System.out.println(name);
            System.out.println(comments.toString());
            System.out.println(dueDate);
            System.out.println(assignee);
            System.out.println("---------------");
        }

    }


    @Test
    @Transactional
    public void getComments() {
        String id = "20006";

        Task task = taskService.createTaskQuery().taskId(id).singleResult();
        String processInstanceId = task.getProcessInstanceId();
        Authentication.setAuthenticatedUserId(String.valueOf(18));
        taskService.addComment(id, processInstanceId, "驳回　材料不足");
        Map<String, Object> map = new HashMap<>();
        map.put("result", "0");
        taskService.complete("", map);

    }

    @Test
    public void list() {
        String userId = "18";
        String taskId = "20006";
        List<Task> list = taskService.createTaskQuery().taskId(taskId).taskCandidateUser(userId).list();
        for (Task task : list) {
            System.out.println(task.getId() + " name " + task.getName());
        }
    }

    @Test
    public void c() {
        String userId = "18";
        String taskId = "20006";
        taskService.claim(taskId, userId);
    }

    @Test
    public void ge() {
        String userId = "18";
        List<Task> list = taskService.createTaskQuery().taskAssignee(userId).list();
        for (Task task : list) {
            System.out.println(task.getId() + " name " + task.getName());
        }
    }

}
